//
//  TouchAllAtOnceScene.cpp
//  SimpleGame
//
//  Created by Scar1et on 2017/8/2.
//
//

// 头文件 -- 类库声明请放入头文件
#include "TouchAllAtOnceScene.h"

// 层级：影响显示与事件响应
enum {
    // 背景
    levelBGSimple = 1,
    // 内容
    levelContentSimple = 101,
    // 对话框
    levelDialogSimple = 1001
};

// TAG：视图标记
enum {
    tagBack = 1,
    tagIconSpiritLeft = 2,
    tagIconSpiritRight = 3
};

// 创建场景
Scene* TouchAllAtOnceScene::createScene() {
    Scene* sTouchAllAtOnce = Scene::create();
    sTouchAllAtOnce->addChild(TouchAllAtOnceScene::create());
    return sTouchAllAtOnce;
}

// 初始化场景，自动调用
bool TouchAllAtOnceScene::init() {
    // 先调用父类Init
    if (!Layer::init()) {
        return false;
    }
    
    // 手机按键监听器
    listenerKeyboard = EventListenerKeyboard::create();
    listenerKeyboard->onKeyReleased = CC_CALLBACK_2(TouchAllAtOnceScene::onKeyReleased, this);
    // 添加到事件分发器中
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerKeyboard, this);
    
    // 多点触控监听器
    listenerTouchAllAtOnce = EventListenerTouchAllAtOnce::create();
    // 是否吞噬【不向下传递】事件
    listenerTouchAllAtOnce->setEnabled(true);
    // 开始
    listenerTouchAllAtOnce->onTouchesBegan = CC_CALLBACK_2(TouchAllAtOnceScene::onTouchesBegan, this);
    // 移动
    listenerTouchAllAtOnce->onTouchesMoved = CC_CALLBACK_2(TouchAllAtOnceScene::onTouchesMoved, this);
    // 取消
    listenerTouchAllAtOnce->onTouchesCancelled = CC_CALLBACK_2(TouchAllAtOnceScene::onTouchesCancelled, this);
    // 结束
    listenerTouchAllAtOnce->onTouchesEnded = CC_CALLBACK_2(TouchAllAtOnceScene::onTouchesEnded, this);
    //添加监听器到事件分发器中
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerTouchAllAtOnce, this);
    
    // 开始初始化
    // 成员变量初始化
    // 可视区域尺寸
    sizeVisible = Director::getInstance()->getVisibleSize();
    // 可视区域的出发点坐标
    positionOrigin = Director::getInstance()->getVisibleOrigin();
    // 屏幕尺寸
    sizeWin = Director::getInstance()->getWinSize();
    
    this->initContent();
    
    // 初始化成功
    return true;
}

// 内容初始化
void TouchAllAtOnceScene::initContent() {
    // 返回按钮
    auto lBack = Label::createWithSystemFont("返回", "arial.ttf", 28);
    lBack->setTextColor(Color4B::WHITE);
    lBack->setHorizontalAlignment(TextHAlignment::LEFT);
    lBack->setVerticalAlignment(TextVAlignment::CENTER);
    lBack->setDimensions(120, 40);
    auto milBack = MenuItemLabel::create(lBack, CC_CALLBACK_1(TouchAllAtOnceScene::onClicked, this));
    milBack->setTag(tagBack);
    milBack->setPosition(Vec2(positionOrigin.x + 80, positionOrigin.y + sizeVisible.height - 40));
    
    // 按钮菜单 -- Widget【可添加多个，用NULL结尾】
    auto mTouchAllAtOnce = Menu::create(milBack, NULL);
    mTouchAllAtOnce->setPosition(Vec2::ZERO);
    this->addChild(mTouchAllAtOnce, levelContentSimple);
    
    // 精灵图标 -- 左
    auto sIconSpiritLeft = Sprite::create("icon_spirit.png");
    sIconSpiritLeft->setPosition(Vec2(positionOrigin.x + sizeVisible.width / 2 - 40, positionOrigin.y + sizeVisible.height / 2));
    this->addChild(sIconSpiritLeft, levelContentSimple, tagIconSpiritLeft);
    
    // 精灵图标 -- 右
    auto sIconSpiritRight = Sprite::create("icon_spirit.png");
    sIconSpiritRight->setPosition(Vec2(positionOrigin.x + sizeVisible.width / 2 + 40, positionOrigin.y + sizeVisible.height / 2));
    this->addChild(sIconSpiritRight, levelContentSimple, tagIconSpiritRight);
}

// 返回功能
void TouchAllAtOnceScene::back() {
    // 从事件分发器移除多点触控监听器
    Director::getInstance()->getEventDispatcher()->removeEventListener(listenerTouchAllAtOnce);
    // 从事件分发器移除手机按键监听器
    Director::getInstance()->getEventDispatcher()->removeEventListener(listenerKeyboard);
    // 从场景堆栈中pop出一个场景，替换现在正运行的场景，而运行中的场景将被删除
    Director::getInstance()->popScene();
}

// 点击监听
void TouchAllAtOnceScene::onClicked(Ref* rSender) {
    int tag = ((MenuItem*)rSender)->getTag();
    switch (tag) {
        case tagBack:
            // 返回按钮被点击
            this->back();
            break;
        default:
            break;
    }
}

// 键盘点击监听
void TouchAllAtOnceScene::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event) {
    switch (keyCode){
        case EventKeyboard::KeyCode::KEY_BACK:
            // 返回键
            this->back();
            break;
        default:
            break;
    }
}

// 触控
// 开始
void TouchAllAtOnceScene::onTouchesBegan(const std::vector<Touch*>& touches, cocos2d::Event* event) {
    log("TouchAllAtOnce：%s", "onTouchesBegan");
}

// 移动
void TouchAllAtOnceScene::onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event* event) {
    log("TouchAllAtOnce：%s", "onTouchesMoved");
    // 遍历touches
    for(int index; index < touches.size(); index++) {
        // 获取触点后，就和单点一样的处理了
        Touch* touch = touches.at(index);
        log("TouchAllAtOnce：touchID = %d", touch->getID());
        log("TouchAllAtOnce：x = %f, y = %f", touch->getLocation().x, touch->getLocation().y);
        // 判断触碰到的控件
        if(touch->getID() == 0) {
            // 触摸到左边按钮
            // 精灵 -- 左边
            auto sIconSpiritLeft = this->getChildByTag(tagIconSpiritLeft);
            // 移动到手触碰的位置
            sIconSpiritLeft->setPosition(touch->getLocation());
        } else if(touch->getID() == 1) {
            // 触摸到右边按钮
            // 精灵 -- 右边边
            auto sIconSpiritRight = this->getChildByTag(tagIconSpiritRight);
            // 移动到手触碰的位置
            sIconSpiritRight->setPosition(touch->getLocation());
        }
    }
}

// 取消，一般是系统层级的消息，如来电话，触摸事件就会被打断
void TouchAllAtOnceScene::onTouchesCancelled(const std::vector<Touch*>& touches, cocos2d::Event* event) {
    log("TouchAllAtOnce：%s", "onTouchesCancelled");
}

// 结束
void TouchAllAtOnceScene::onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event* event) {
    log("TouchAllAtOnce：%s", "onTouchesEnded");
}
